<?php
/**
 * PageTitle class file.
 * @author Christoffer Niska <ChristofferNiska@gmail.com>
 * @copyright Copyright &copy; Christoffer Niska 2011-
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 */

/**
 * Page title allows for setting dynamic titles.
 */
class PageTitle extends CWidget
{
	/**
	 * @property array the page title parts.
	 */
	public $parts;
	/**
	 * @property string the page title separator.
	 */
	public $separator = ' | ';
	/**
	 * @property boolean whether to append the application name to the title.
	 */
	public $appendAppName = true;

	/**
	 * Runs the widget.
	 */
	public function run()
	{
		$controller=$this->getController();

		if(!empty($this->parts))
			$parts=$this->parts;
		else
		{
			$parts=array();
			$name=ucfirst($controller->getId());
			$action=$controller->getAction();
			$module=$module=$controller->getModule();

			if($action!==null && strcasecmp($action->getId(),$controller->defaultAction))
				$parts[]=ucfirst($action->getId()).' '.$name;
			else if ($module!==null && strcasecmp($name,$module->defaultController))
				$parts[]=$name;

			if($module!==null)
				$parts[]=ucfirst($module->getId());
		}

		if ($this->appendAppName)
			$parts[]=Yii::app()->name;

		$title=implode($parts, $this->separator);
		$controller->setPageTitle($title);
	}
}